# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: leon

from typing import Union, List
from coin.exchange.binance_futures.ws.subscriber import BinanceFuturesSubscriber
from coin.exchange.binance_futures.rest.public import BinanceBookQueryer
from coin.strategy.mm.feed_builder import (
    BinanceFuturesFlowBookBuilder,
    BinanceFuturesSnapshotL1RealtimeFlowBookBuilder,
)
from coin.strategy.mm.subscription import SubscriptionRequest
from coin.strategy.mm.base.feed import BaseFeedSubsystem


class BinanceFeedSubscriberWrapper(object):
  def __init__(self,
               queue,
               group_id: str,
               worker_id: str,
               symbols: Union[List[str], str],
               close_callback=None,
               logger=None):
    self._subscriber = BinanceFuturesSubscriber(queue,
                                                group_id,
                                                worker_id,
                                                symbols,
                                                close_callback,
                                                logger)
    self._book_queryer = BinanceBookQueryer(queue, worker_id, symbols, logger=logger)

  def start(self, *args, **kwargs):
    self._subscriber.start(*args, **kwargs)
    self._book_queryer.start(*args, **kwargs)

  def stop(self, *args, **kwargs):
    self._subscriber.stop(*args, **kwargs)
    self._book_queryer.stop()


class BinanceSwapFuturesFeedSubsystem(BaseFeedSubsystem):
  FeedSubReq = SubscriptionRequest('Futures', 'Binance', None)
  FeedSubscriber = BinanceFeedSubscriberWrapper
  FlowBookBuilder = BinanceFuturesFlowBookBuilder


class BinanceSwapFuturesSnapshotL1RealtimeFeedSubsystem(BaseFeedSubsystem):
  FeedSubReq = SubscriptionRequest('Futures', 'Binance', 'l1_realtime')
  FeedSubscriber = BinanceFeedSubscriberWrapper
  FlowBookBuilder = BinanceFuturesSnapshotL1RealtimeFlowBookBuilder
